#include "curtain_control.h"
#include "ui_curtain_control.h"
#include "mainwindow.h"
#include "all_drive.h"

#include <QDebug>
#include"sqlitedboperator.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <string.h>
#include <fcntl.h>                          // 提供open()函数
#include <sys/types.h>                      // 提供mode_t类型
#include <sys/stat.h>                       // 提供open()函数的符号

#define GEC6818_ADC_IN0		_IOR('A',  1, unsigned long) //adc0
#define GEC6818_ADC_IN1		_IOR('A',  2, unsigned long)  //adc1

SqliteDBAOperator MyDB;

all_drive a_dr_c;

extern int curtain_arr[5]; // 上一次的{ 门位置，上升过载状态 ， 下降过载状态}

extern int MOD_MESSAGE_TAB[4];

extern int kbdfd,ledsfd,beepfd,adcfd;

Curtain_Control::Curtain_Control(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Curtain_Control)
{

    ui->setupUi(this);

    startInit();
}

Curtain_Control::~Curtain_Control()
{
    delete ui;
}

void Curtain_Control::startInit()
{
    this->setWindowState(Qt::WindowNoState);
    ui->stackedWidget->setCurrentIndex(0);

    curtain_down = 0;
    curtain_up = 0;
    curtain_down = 0;
    adc_count = false;

    Image_greenon.load(":/images/greenon.gif");
    pixmap_greenon = QPixmap::fromImage(Image_greenon);
    fitpixmap_greenon = pixmap_greenon.scaled(72, 32, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);

    Image_greenoff.load(":/images/greenoff.gif");
    pixmap_greenoff = QPixmap::fromImage(Image_greenoff);
    fitpixmap_greenoff = pixmap_greenoff.scaled(72, 32, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);

    Image_redon.load(":/images/redon.gif");
    pixmap_redon = QPixmap::fromImage(Image_redon);
    fitpixmap_redon = pixmap_redon.scaled(72, 32, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);

    Image_redoff.load(":/images/redoff.gif");
    pixmap_redoff = QPixmap::fromImage(Image_redoff);
    fitpixmap_redoff = pixmap_redoff.scaled(72, 32, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);

    oc_TimeoutTimer = new QTimer(this);//创建一个定时器
    connect(oc_TimeoutTimer, SIGNAL(timeout()), this, SLOT(curtain_oc_Timeout()));//链接按键扫描定时器信号给oc_Timeout函数
    KbdScanTimer = new QTimer(this);//创建一个定时器
    connect(KbdScanTimer, SIGNAL(timeout()), this, SLOT(deal_key()));//链接按键扫描定时器信号给KbdLedScan函数

    if(kbdfd>0)
    {
        KbdScanTimer->start(100);//开启100ms的按键和LED状态扫描
    }

    adcScanTimer = new QTimer(this);//创建一个定时器
    connect(adcScanTimer, SIGNAL(timeout()), this, SLOT(deal_adc()));//链接按键扫描定时器信号给KbdLedScan函数

    if(adcfd>0)
    {
        adcScanTimer->start(1000);//开启100ms的按键和LED状态扫描
    }

    QPointF  point(157,152);
    curtain1 = new MyPixMapItem(":/images/curtain.png");
    curtain2 = new MyPixMapItem(":/images/curtain2.png");
    background= new MyPixMapItem(":/images/background.jpg");
    scene.addItem(background);
    scene.addItem(curtain1);
    scene.addItem(curtain2);

    //curtain1->setPos(-(background->pix.width()/2)+(curtain1->pix.width()/2)+157-218+30,-(background->pix.height()/2)+(curtain2->pix.height()/2)+152-80);

    curtain2->setPos(-(background->pix.width()/2)+(curtain1->pix.width()/2)+157-218,-(background->pix.height()/2)+(curtain2->pix.height()/2)+152-80);

    curtain1->setYEn(true);
    curtain1->setXEn(true);

    curtain_open_percent = curtain_arr[0]; //上一次门的位置
    Open_the_overload = curtain_arr[1];  //上一次是否上升过载
    Close_the_overload = curtain_arr[2];  //上一次是否下降过载

    if(Open_the_overload || Close_the_overload ) //初始化过载图片和灯
    {a_dr_c.LEDsSetValue(1,3);
//    ui->label_img_alarm->setPixmap(fitpixmap_redon);
    }
    else
    {a_dr_c.LEDsSetValue(0,3);
//    ui->label_img_alarm->setPixmap(fitpixmap_redoff);
    }

    background->setXrate(100.0);
    curtain2->setXrate(100.0);
    curtain2->setXrate(100.0);
    background->setYrate(100.0);

    ui->graphicsView->setScene(&scene);
    ui->graphicsView->show();

//    if(curtain_open_percent == 0)
//        ui->label_img_limn_2->setPixmap(fitpixmap_greenon);
//    else if(curtain_open_percent == 100)
//        ui->label_img_limp_2->setPixmap(fitpixmap_greenon);


    delete_count = 0;

    MOD_message_count = 0;

    port_informationTimer = new QTimer(this);
    connect(port_informationTimer, SIGNAL(timeout()), this, SLOT(deal_message()));//链接按键扫描定时器信号给KbdLedScan函数
    port_informationTimer->start(100);

    //打开数据库
    MyDB.OpenDb();
    //if(!MyDB.IsTaBexists(tablename))
    MyDB.createTable();
    queryTable();
    curtain1->setXrate(curtain_arr[0]);

    settingTimer = new QTimer(this);
    connect(settingTimer,SIGNAL(timeout()), this, SLOT(deal_setting()));
    settingTimer->start(1000);

    movie=new QMovie (this) ;
    movie->setFileName(":/images/cat.gif");
    movie->start();
    ui->airlabel->setMovie(movie);
}

void Curtain_Control::queryTable()
{
    QString str = QString(" select *from EVENTTABLE");
    sqlmodel.setQuery(str);
    ui->tableView->setModel(&sqlmodel);
}

void Curtain_Control::deal_setting()
{
}

void Curtain_Control::curtain_oc_Timeout()
{
    if(curtain_up)  //门进行上升
    {
        if(curtain_open_percent != 0)
        {
            curtain_update = 0;
            QString str = QString("窗帘打开失败(Open the failure) 当前开度%1%").arg(100 - curtain_open_percent);
            if(MOD_message_count == 1)
                sql_insert_data("网络操作(network operation)", str);
            else if(MOD_message_count == 0)
                sql_insert_data("本地操作(local operating)", str);
            qDebug()<<"curtain_oc_Timeout is up timeout";
            oc_Timeout_count = 0;
            Open_the_overload = 1;
//            ui->label_img_upoverload_2->setPixmap(fitpixmap_greenon);
            a_dr_c.LEDsSetValue(1,3);

        }
    }
    if(curtain_down)  //门进行下降
    {
        if(curtain_open_percent != 100)
        {
            curtain_update = 0;
            QString str = QString("窗帘关闭失败(Close the failure) 当前开度%1%").arg(100 - curtain_open_percent);
            if(MOD_message_count == 2)
                sql_insert_data("网络操作(network operation)", str);
            else if(MOD_message_count == 0)
                sql_insert_data("本地操作(local operating)", str);
            qDebug()<<"curtain_oc_Timeout is down timeout";
            oc_Timeout_count = 0;
            Close_the_overload = 1;
//            ui->label_img_downoverload_2->setPixmap(fitpixmap_greenon);
            a_dr_c.LEDsSetValue(1,3);
        }
    }
    if(Open_the_overload ||Close_the_overload)
    {
        curtain_update=0;
//        ui->label_img_alarm->setPixmap(fitpixmap_redon);
        a_dr_c.LEDsSetValue(1,3);  //led4 报警灯
        for(int i = 0;i<3;i++)  //蜂鸣器鸣叫三次
        {
            ioctl(beepfd,1,1);
            sleep_ms(100);
            ioctl(beepfd,0,1);
            sleep_ms(100);
        }
    }
    else
    {
        curtain_update=1;
//        ui->label_img_alarm->setPixmap(fitpixmap_redoff);
        a_dr_c.LEDsSetValue(0,3);; //led4 报警灯
    }


    MOD_message_count = 0;
    oc_TimeoutTimer->stop();

}

void Curtain_Control::deal_adc() //adc采样服务程序
{
    if(adc_count == false)
        return;
    if(!(adcfd<0))
    {
        rt=ioctl(adcfd,GEC6818_ADC_IN0,&adc_vol);
        qDebug()<<"adc in0 vol: "<<adc_vol;
    }
    /*
    if(adc_vol >10000)
        curtain_open_percent = 100;
    else
        curtain_open_percent=adc_vol/1000;
    curtain1->setXrate(curtain_open_percent);
    */



}


void Curtain_Control::deal_key()
{
    if(!(kbdfd<0))//按键驱动可用，读取按键状态
    {
        read(kbdfd,&KbdStatus,1);
        if(Last_key_value == KbdStatus)  //按一下触发一次
        {
            return;
        }

        switch (KbdStatus)
        {
        case 1:   //第一个按键  //按键1控制的关闭限位
            {
            if(Open_the_overload || Close_the_overload)
                return;
            Closed_in_place = (Closed_in_place +1)%2;
            Last_key_value = 1;
            curtain_open_percent = 100;
            curtain1->setXrate(curtain_open_percent);
            curtain_update = 0;
            break;}
        case 2:   //第二个按键  //按键2控制的打开限位
        {if(Open_the_overload || Close_the_overload)
                return;
            Last_key_value = 2;
            Open_in_place =(Open_in_place +1)%2;
            curtain_open_percent = 0;
            curtain1->setXrate(curtain_open_percent);
            curtain_update = 0;
            break;}
        case 4:   //第三个按键  //按键3控制的关闭过载
        {Close_the_overload = (Close_the_overload + 1)%2;
            Last_key_value = 4;
//            if(Close_the_overload % 2 ==0)
//            {
//                ui->label_img_downoverload_2->setPixmap(fitpixmap_greenoff);
//            }
//            else
//            {
//                ui->label_img_downoverload_2->setPixmap(fitpixmap_greenon);
//            }
            break;}
        case 8:  //第四个按键  //按键4控制的打开过载
        {Open_the_overload =(Open_the_overload+ 1)%2;
            Last_key_value = 8;
            if(Open_the_overload % 2 ==0)
//            {
//                ui->label_img_upoverload_2->setPixmap(fitpixmap_greenoff);
//            }
//            else
//            {
//                ui->label_img_upoverload_2->setPixmap(fitpixmap_greenon);
//            }
            break;
        default:
                Last_key_value = 0;
                break;}
        }
        if(Open_the_overload ||Close_the_overload)
        {
            curtain_update=0;
//            ui->label_img_alarm->setPixmap(fitpixmap_redon);
            a_dr_c.LEDsSetValue(1,3);  //led4 报警灯
            for(int i = 0;i<3;i++)  //蜂鸣器鸣叫三次
            {
                ioctl(beepfd,1,1);
                sleep_ms(100);
                ioctl(beepfd,0,1);
                sleep_ms(100);
            }
        }
        else
        {
            curtain_update=1;
//            ui->label_img_alarm->setPixmap(fitpixmap_redoff);
            a_dr_c.LEDsSetValue(0,3);; //led4 报警灯
        }

    }
}

void Curtain_Control::deal_message()
{
        switch (MOD_MESSAGE_TAB[0])
        {
        case 1:
            {
                MOD_message_count = 1;
                on_pushButton_up_clicked();
                break;
            }
        case 2:
            {
                MOD_message_count = 2 ;
                on_pushButton_down_clicked();
                break;
            }
        default:
            {
                break;
            }
        }
}


void Curtain_Control::sql_insert_data(QString type, QString message)  //插入数据库的函数
{
    current_time=gaintime();
    w2dba record;
    record.id=1; //设置自增了，所以这个值不修改
    record.date=current_time;
    record.type=type;
    record.message=message;
    MyDB.singleinsertdata(tablename,record);//插入一条数据
    queryTable();
}

void Curtain_Control::sleep_ms(unsigned int msec) //延时函数 ms
{
    QTime reachTime = QTime::currentTime().addMSecs(msec);
    while(QTime::currentTime() <reachTime)
        QCoreApplication::processEvents(QEventLoop::AllEvents,100);
}

QString Curtain_Control::gaintime()  //获取当前时间的函数
{
    QDateTime current_date_time =QDateTime::currentDateTime();
    QString current_time =current_date_time.toString("yyyy.MM.dd hh:mm:ss ddd");
    return current_time;
}

void Curtain_Control::on_pushButton_mainPage_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
    ui->mainlable->setStyleSheet("background-color: rgb(0, 0, 255);\nborder-radius:20px;\n" );
    ui->settinglable->setStyleSheet("background-color: rgb(0, 170, 255);\nborder-radius:20px;\n" );
    ui->jilulable->setStyleSheet("background-color: rgb(0, 170, 255);\nborder-radius:20px;\n" );
}

void Curtain_Control::on_Parameter_set_Button_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
    ui->mainlable->setStyleSheet("background-color: rgb(0, 170, 255);\nborder-radius:20px;\n" );
    ui->settinglable->setStyleSheet("background-color: rgb(0, 0, 255);\nborder-radius:20px;\n" );
    ui->jilulable->setStyleSheet("background-color: rgb(0, 170, 255);\nborder-radius:20px;\n" );
    ui->lineEdit_uptime->setText(QString::number(curtain_time_up));
    ui->lineEdit_downtime->setText(QString::number(curtain_time_down));
    ui->lineEdit_max_uptime->setText(QString::number(max_uptime/1000));
    ui->lineEdit_max_downtime->setText(QString::number(max_downtime/1000));

}

void Curtain_Control::on_Alarm_information_Button_clicked()
{
    ui->stackedWidget->setCurrentIndex(2);
    ui->mainlable->setStyleSheet("background-color: rgb(0, 170, 255);\nborder-radius:20px;\n" );
    ui->settinglable->setStyleSheet("background-color: rgb(0, 170, 255);\nborder-radius:20px;\n" );
    ui->jilulable->setStyleSheet("background-color: rgb(0, 0, 255);\nborder-radius:20px;\n" );

}



void Curtain_Control::on_Button_backui_clicked()
{
    if( Open_the_overload || Close_the_overload )
    {
        curtain_arr[1]=Open_the_overload;
        curtain_arr[2]=Close_the_overload;
        Open_the_overload = 0;
        Close_the_overload = 0;
        curtain_up = false;
        curtain_down = false;
    }
    if(curtain_up)
    {
        curtain_open_percent = 1;
        on_pushButton_up_clicked();
        delete_count = 1;

    }
    else if(curtain_down)
    {
        curtain_open_percent = 99;
        on_pushButton_down_clicked();
        delete_count = 1;

    }

    KbdScanTimer->stop();
    adcScanTimer->stop();
    oc_TimeoutTimer->stop();
    curtain_arr[0]=curtain_open_percent;
    this->parentWidget()->show();
    this->close();
}

void Curtain_Control::on_pushButton_up_clicked()
{
    int i=curtain_open_percent;
    curtain_update = 1;
    curtain_down = false;
    curtain_up=true;

    if(i == 0 || Open_the_overload == 1 || Close_the_overload == 1) //如果当前门开到最大直接退出
        return;

    if(max_uptime !=0)
    {
        oc_TimeoutTimer->start(max_uptime);
        qDebug()<<"oc_TimeoutTimer->start();";
        oc_Timeout_count = 1;
    }

//    ui->label_img_down->setPixmap(fitpixmap_greenoff);
//    ui->label_img_up->setPixmap(fitpixmap_greenon);
//    ui->label_img_limn_2->setPixmap(fitpixmap_greenoff);
//    ui->label_img_limp_2->setPixmap(fitpixmap_greenoff);
    ui->dongzuolabel->setText("正在打开");
    a_dr_c.LEDsSetValue(1,0);
    a_dr_c.LEDsSetValue(0,1);
    a_dr_c.LEDsSetValue(1,2);

    for(int j=0; j<i ; j++)
     {
        curtain_open_percent=curtain_open_percent - 1.0;
        sleep_ms(curtain_time_up);
        curtain1->setXrate(curtain_open_percent);
        ui->zhuangtailabel->setText(QString::number(100-curtain_open_percent)+"%");

        //qDebug()<<"percent="<<curtain_open_percent;
         if(curtain_update==0 || curtain_up == false || curtain_open_percent == 0)
         {

             //LEDsSetValue(0,0); //led0 打开窗帘的灯
//             ui->label_img_up->setPixmap(fitpixmap_greenoff);
             j=i+1;
             ui->dongzuolabel->setText("无");
             a_dr_c.LEDsSetValue(0,0);
             a_dr_c.LEDsSetValue(0,2);
         }

     }

    a_dr_c.LEDsSetValue(0,0);
    a_dr_c.LEDsSetValue(0,2);

//    ui->label_img_up->setPixmap(fitpixmap_greenoff);
//    if(curtain_open_percent == 0)
//        ui->label_img_limn_2->setPixmap(fitpixmap_greenon);
//    else if(curtain_open_percent == 100)
//        ui->label_img_limp_2->setPixmap(fitpixmap_greenon);

    curtain_update = 0;
    curtain_up = false;
    curtain_down = false;

    if(MOD_message_count == 1)
    {
        sql_insert_data("网络操作(network operation)", "窗帘打开(Open the curtains)");
    }
    else if(MOD_message_count == 0)
    {
        sql_insert_data("本地操作(local operating)", "窗帘打开(Open the curtains)");
    }


    if(delete_count)
    {
        on_Button_backui_clicked();
    }


}

void Curtain_Control::on_pushButton_down_clicked()
{
    int i=100 - curtain_open_percent;
    curtain_down =true;
    curtain_up=false;

    curtain_update = 1;

    if(i == 0 || Close_the_overload == 1 || Open_the_overload == 1)//如果当前门关闭状态直接退出
        return;

    if(max_downtime !=0)
    {
        oc_TimeoutTimer->start(max_downtime);
        qDebug()<<"oc_TimeoutTimer->start();";
        oc_Timeout_count = 1;
    }

//    ui->label_img_down->setPixmap(fitpixmap_greenon);
//    ui->label_img_up->setPixmap(fitpixmap_greenoff);
//    ui->label_img_limn_2->setPixmap(fitpixmap_greenoff);
//    ui->label_img_limp_2->setPixmap(fitpixmap_greenoff);
    ui->dongzuolabel->setText("正在关闭");
    a_dr_c.LEDsSetValue(1,1);
    a_dr_c.LEDsSetValue(0,1);
    a_dr_c.LEDsSetValue(1,2);

    for(int j=0; j<i ; j++)
    {
        curtain_open_percent=curtain_open_percent + 1.0;
        sleep_ms(curtain_time_down);
        curtain1->setXrate(curtain_open_percent);
        ui->zhuangtailabel->setText(QString::number(100-curtain_open_percent)+"%");
        //qDebug()<<"percent="<<curtain_open_percent;
        if(curtain_update==0 || curtain_down == false || curtain_open_percent == 100)
        {
//            ui->label_img_down->setPixmap(fitpixmap_greenoff);
            j=i+1;
            ui->dongzuolabel->setText("无");
            a_dr_c.LEDsSetValue(0,1);
            a_dr_c.LEDsSetValue(0,2);
        }

    }

    a_dr_c.LEDsSetValue(0,1);
    a_dr_c.LEDsSetValue(0,2);
//    ui->label_img_up->setPixmap(fitpixmap_greenoff);
//    if(curtain_open_percent == 0)
//        ui->label_img_limn_2->setPixmap(fitpixmap_greenon);
//    else if(curtain_open_percent == 100)
//        ui->label_img_limp_2->setPixmap(fitpixmap_greenon);

    if(MOD_message_count == 2)
    {
        sql_insert_data("网络操作(network operation)", "窗帘关闭(curtain closed)");
    }
    else if(MOD_message_count == 0)
    {
        sql_insert_data("本地操作(local operating)", "窗帘关闭(curtain closed)");
    }

    curtain_update = 0;
    curtain_up = false;
    curtain_down = false;
    if(delete_count)
        on_Button_backui_clicked();
}

void Curtain_Control::on_pushButton_stop_clicked()
{
    ioctl(beepfd,0,1);
    Close_the_overload = 0;
    Open_the_overload = 0;
//    ui->label_img_upoverload_2->setPixmap(fitpixmap_greenoff);  //关闭图片灯
//    ui->label_img_downoverload_2->setPixmap(fitpixmap_greenoff);
//    ui->label_img_alarm->setPixmap(fitpixmap_redoff);

//    ui->label_img_up->setPixmap(fitpixmap_greenoff);  //关闭图片灯
//    ui->label_img_down->setPixmap(fitpixmap_greenoff);
//    if(curtain_open_percent == 0)
//        ui->label_img_limn_2->setPixmap(fitpixmap_greenon);
//    else if(curtain_open_percent == 100)
//        ui->label_img_limp_2->setPixmap(fitpixmap_greenon);
    curtain_update = 0;
}

void Curtain_Control::on_lineEdit_uptime_cursorPositionChanged(int arg1, int arg2)
{
}

void Curtain_Control::on_lineEdit_downtime_cursorPositionChanged(int arg1, int arg2)
{

}

void Curtain_Control::on_lineEdit_max_uptime_cursorPositionChanged(int arg1, int arg2)
{
}

void Curtain_Control::on_lineEdit_max_downtime_cursorPositionChanged(int arg1, int arg2)
{
}

void Curtain_Control::on_pushButton_query_clicked()
{
    QString Query_mode , str ,text,Query_text;

    if(ui->comboBox->currentIndex() == 0)
    {
        queryTable();
        return;
    }
    else if(ui->comboBox->currentIndex() == 1)
    {
        Query_mode = "ID";
        Query_text =ui->lineEdit_query_text->text();
    }
    else if(ui->comboBox->currentIndex() == 2)
    {
        Query_mode = "DATE";
        text=ui->lineEdit_query_text->text();
        Query_text = QString("%%%1%%").arg(text);
    }
    else if(ui->comboBox->currentIndex() == 3)
    {
        Query_mode = "TYPE";
        text=ui->lineEdit_query_text->text();
        Query_text = QString("%%%1%%").arg(text);
    }
    else if(ui->comboBox->currentIndex() == 4)
    {
        Query_mode = "MESSAGE";
        text=ui->lineEdit_query_text->text();
        Query_text = QString("%%%1%%").arg(text);
    }


    str = QString("select *from EVENTTABLE WHERE %1 like '%2'").arg(Query_mode).arg(Query_text);
    qDebug()<<"str="<<str;
    sqlmodel.setQuery(str);
    ui->tableView->setModel(&sqlmodel);
}

void Curtain_Control::on_Button_adc_clicked()
{
    if(adc_count)
        adc_count = false;
    else
        adc_count = true;
}

void Curtain_Control::on_lineEdit_downtime_editingFinished()
{
    if((ui->lineEdit_downtime->text() == "") || (ui->lineEdit_downtime->text().toInt()<10))
        curtain_time_down=10;
    else
        curtain_time_down=ui->lineEdit_downtime->text().toInt();
    max_downtime=curtain_time_down*100;

    ui->lineEdit_uptime->setText(QString::number(curtain_time_up));
    ui->lineEdit_downtime->setText(QString::number(curtain_time_down));
    ui->lineEdit_max_uptime->setText(QString::number(max_uptime/1000));
    ui->lineEdit_max_downtime->setText(QString::number(max_downtime/1000));

}

void Curtain_Control::on_lineEdit_uptime_editingFinished()
{
    if((ui->lineEdit_uptime->text() == "") || (ui->lineEdit_uptime->text().toInt()<10))
        curtain_time_up=10;
    else
        curtain_time_up=ui->lineEdit_uptime->text().toInt();
    max_uptime=curtain_time_up*100;
    ui->lineEdit_uptime->setText(QString::number(curtain_time_up));
    ui->lineEdit_downtime->setText(QString::number(curtain_time_down));
    ui->lineEdit_max_uptime->setText(QString::number(max_uptime/1000));
    ui->lineEdit_max_downtime->setText(QString::number(max_downtime/1000));


}

void Curtain_Control::on_lineEdit_max_uptime_editingFinished()
{
    if((ui->lineEdit_max_uptime->text() == "") )
        max_uptime=0;
    else
        max_uptime=(ui->lineEdit_max_uptime->text().toInt()*1000);
    curtain_time_up=max_uptime/100;
    ui->lineEdit_uptime->setText(QString::number(curtain_time_up));
    ui->lineEdit_downtime->setText(QString::number(curtain_time_down));
    ui->lineEdit_max_uptime->setText(QString::number(max_uptime/1000));
    ui->lineEdit_max_downtime->setText(QString::number(max_downtime/1000));


}

void Curtain_Control::on_lineEdit_max_downtime_editingFinished()
{
    if((ui->lineEdit_max_downtime->text() == "") )
        max_downtime=0;
    else
        max_downtime=(ui->lineEdit_max_downtime->text().toInt()*1000);
    curtain_time_down=max_downtime/100;
    ui->lineEdit_uptime->setText(QString::number(curtain_time_up));
    ui->lineEdit_downtime->setText(QString::number(curtain_time_down));
    ui->lineEdit_max_uptime->setText(QString::number(max_uptime/1000));
    ui->lineEdit_max_downtime->setText(QString::number(max_downtime/1000));

}
